﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;

namespace GUI
{
    public partial class TinRaoVatForm : Form
    {
        public int MaNguoiDung {get;set;}
        public string PhanQuyen { get; set; }
        public bool ViewAbuse { get; set; }

        public TinRaoVatForm()
        {
            InitializeComponent();
            
        }
        private void loadComboboxData()
        {

            //Load tin rao vat
            BindingList<TinRaoVat> listTin;

            if (PhanQuyen.Equals("Administrator") || PhanQuyen.Equals("Moderator"))
            {
                if (ViewAbuse)
                    listTin = new TinRaoVatBUS().LoadAbuse();
                else
                    listTin = new TinRaoVatBUS().LoadAll();
                chkAbuse.Enabled = ViewAbuse;
            }
            else
                listTin = new TinRaoVatBUS().LoadAllByNguoiTao(MaNguoiDung);
            //hien thi len combobox
            cmbList.DataSource = listTin;
            cmbList.DisplayMember = "HienThi";
            cmbList.ValueMember = "MaTin";

            //Load khuvuc
            BindingList<KhuVuc> litKV = new KhuVucBUS().LoadAll();
            //hien thi len combobox
            cbxKhuVuc.DataSource = litKV;
            cbxKhuVuc.DisplayMember = "TenKhuVuc";
            cbxKhuVuc.ValueMember = "MaKhuVuc";

            //Load Nhom
            BindingList<NhomTin> listNhom = new NhomTinBUS().LoadAll();
            //hien thi len combobox
            cbxNhom.DataSource = listNhom;
            cbxNhom.DisplayMember = "TenNhomTin";
            cbxNhom.ValueMember = "MaNhomTin";

            ////Load NguoiTao
            BindingList<NguoiDung> listNguoiTao = new NguoiDungBUS().LoadAll();
            //hien thi len combobox
            cbxNguoiTao.DataSource = listNguoiTao;
            cbxNguoiTao.DisplayMember = "HoTenDangNhap";
            cbxNguoiTao.ValueMember = "MaNguoiDung";
            cbxNguoiTao.SelectedValue = MaNguoiDung;

        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            loadComboboxData();
            btnDel.Enabled = true;
            btnUpdate.Enabled = true;
        }

        private void cmbList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingList<TinRaoVat> list = cmbList.DataSource as BindingList<TinRaoVat>;
            int index = cmbList.SelectedIndex;
            if ((0 <= index) && (index < list.Count))
            {
                TinRaoVat sv = list[index];
                txtMa.Text = sv.MaTin.ToString();
                txtTieuDe.Text = sv.TieuDe;
                cbxKhuVuc.SelectedValue = sv.MaKhuVuc;
                cbxNhom.SelectedValue = sv.MaNhom;
                calendarNgayHetHan.Text = sv.NgayHetHan.ToString();
                txtNoiDung.Text = sv.NoiDung;
                txtNgayTao.Text = sv.NgayTao.ToString();
                txtSoLanXem.Text = sv.SoLanXem.ToString();
                chkAbuse.Checked = (bool) sv.abuse;
                chkTrangThai.Checked = (bool)sv.TrangThai;
                cbxNguoiTao.SelectedValue = sv.NguoiTao.ToString();

            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int id = int.Parse(txtMa.Text);
            int result = new TinRaoVatBUS().DeleteByStringId(id);
            //load lai data
            loadComboboxData();
            if (result <= 0)
                MessageBox.Show("Khong the xoa ma so: " + id);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (chkUpdate.Checked)
                UpdateSv();
            else
                InsertSv();
            loadComboboxData();

        }

        private void chkUpdate_CheckedChanged(object sender, EventArgs e)
        {

            if (!chkUpdate.Checked)
                btnUpdate.Text = "Thêm";
            else
                btnUpdate.Text = "Cập nhật";
        }
        private void UpdateSv()
        {
            // lay ma so sinh vien can cap nhat
            TinRaoVat sv = CreateSvObj();
            sv.NgayTao = DateTime.Parse(txtNgayTao.Text);
            sv.TrangThai = chkTrangThai.Checked;
            sv.abuse = chkAbuse.Checked;
            sv.SoLanXem = double.Parse(txtSoLanXem.Text);
            int result = new TinRaoVatBUS().Update(sv);
            if (result <= 0)
                MessageBox.Show("Khong the cap nhat.");
        }

        private TinRaoVat CreateSvObj()
        {

           TinRaoVat sv = new TinRaoVat();
            try
            {
                sv.MaTin = int.Parse(txtMa.Text);
            }
            catch { }
            sv.TieuDe = txtTieuDe.Text;
            sv.MaKhuVuc = int.Parse(cbxKhuVuc.SelectedValue.ToString());
            sv.MaNhom = int.Parse(cbxNhom.SelectedValue.ToString());
            sv.NgayHetHan =  DateTime.Parse(calendarNgayHetHan.Text);
            sv.NoiDung = txtNoiDung.Text;
            sv.NguoiTao = int.Parse(cbxNguoiTao.SelectedValue.ToString());
            return sv;
        }

        private void InsertSv()
        {
            TinRaoVat sv = CreateSvObj();
            sv.NgayTao = DateTime.Now;
            sv.TrangThai = true;
            sv.SoLanXem = 0;
            sv.NguoiTao = int.Parse(cbxNguoiTao.SelectedValue.ToString());
            sv.abuse = false;
            int result = new TinRaoVatBUS().Insert(sv);
            if (result <= 0)
                MessageBox.Show("Khong the them.");
        }

        private void TinRaoVatForm_Load(object sender, EventArgs e)
        {
            ViewAbuse = false;
            MaNguoiDung = @LogedIn.login.MaNguoiDung;
            lblPhanQuyen.Text = PhanQuyen;
        }

        private void chkAbuseOnly_CheckedChanged(object sender, EventArgs e)
        {
            ViewAbuse = chkAbuseOnly.Checked;
            loadComboboxData();
        }
    }
}
